package com.hrc.springbootdemo.retry;


import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RetryService {


    @Retryable(
            // 指定重试的异常
            retryFor = {Exception.class},
            // 指定重试的次数
            maxAttempts = 2,
            // 配置重试的间隔时间
            backoff = @Backoff(delay = 1000, multiplier = 2)
    )
    public void  retry() {
           throw new NullPointerException();
        }

      /** 降级操作 根据异常选择对应的方法*/
     @Recover
    public void  doRetry(RuntimeException ex) {
         System.out.println("重试失败，执行恢复逻辑: " + ex.getMessage());
    }

    /** 降级操作*/
    @Recover
    public void  doRetry(NullPointerException ex) {
        System.out.println("重试失败，执行恢复逻辑: " + ex.getMessage());
    }

}
